﻿using AutoMapper;
using B.S.Warehouse.Domain;
using B.S.Warehouse.ErrorCord;
using B.S.Warehouse.Write.Api.Application.Command.AllocationDetailsCommand;
using MediatR;
using SqlSugar;

namespace B.S.Warehouse.Write.Api.Application.Handelr.AllocationDetailsHandler
{
    public class AddAllocationDetailsHandler : IRequestHandler<AddAllocationDetailsCommand, ApiResult<object>>
    {
        private readonly ISqlSugarClient db;
        private readonly IMapper mapper;
        public AddAllocationDetailsHandler(ISqlSugarClient db, IMapper mapper)
        {
            this.db = db;
            this.mapper = mapper;
        }
        public async Task<ApiResult<object>> Handle(AddAllocationDetailsCommand request, CancellationToken cancellationToken)
        {
            ApiResult<object> result = new ApiResult<object>();

            result.Code = ApiEnum.添加成功;
            result.MAG = ApiEnum.添加成功.ToString();
            var model = mapper.Map<AllocationDetails>(request);
            model.IsDel = false;
            await db.Insertable(model).ExecuteCommandAsync();

            return await Task.FromResult(result);
        }
    }
}
